<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">

    <style>
        .red {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container">
            <div class="form-group">
                <div class="input-group">
                    <h4>品牌管理</h4>
                </div>
            </div>
            <table class="table table-bordered table-hover mt-2">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>资产名称</th>
                        <th>价格</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in list">
                        <td>{{ index + 1 }}</td>
                        <td>{{ item.name }}</td>
                        <td :class="{red: item.price >= 100}">{{ item.price }}</td>
                        <td>{{ formatTime( item.time ) }}</td>
                        <td><a @click.prevent="del(index)" href="#">删除</a></td>
                    </tr>
                </tbody>
            </table>
            <form class="form-inline">
                <div class="form-group">
                    <div class="input-group"><input type="text" v-model="name" placeholder="资产名称" class="form-control">
                    </div>
                </div>
                <p></p>
                <div class="form-group">
                    <div class="input-group"><input type="text" v-model="price" placeholder="价格" class="form-control">
                    </div>
                </div>
                <p></p>
                <!-- button在form里就是提交，提交就会跳转，没跳走是没有写路径，没有写路径就是原地刷新 -->
                <button @click.prevent="add" class="btn btn-primary">添加资产</button>
            </form>
        </div>
    </div>

    <script src="./vue.js"></script>
    <script src="./dayjs.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                // 双向绑定输入的资产名字
                name: '',
                // 双向绑定输入的价格
                price: '',
                list: [
                    { name: '可爱多', price: 2.5, time: new Date() },
                    { name: '哈根达斯', price: 25, time: new Date() },
                ]
            },
            methods: {
                add() {
                    // 非空判断
                    if (this.name == '' || this.price == '') return alert('请输入完整内容')
                    // 如果不为空才加到数组
                    this.list.push({
                        name: this.name,
                        price: this.price,
                        time: new Date()
                    })
                    // 添加完成清空输入内容
                    this.name = this.price = ''
                },
                // 删除的点击事件
                del (index) {

                   // 删除数组中这个下标对应的元素
                   this.list.splice(index, 1)
                },

                formatTime (val) {
                
                    return dayjs(val).format('YYYY-MM-DD')
                }
            }
        })
    </script>
</body>

</html>